package com.heima.admin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {
    @Override
    public PageResponseResult findByQuery(ChannelDTO dto) {
        // 1. 校验参数
        dto.checkParam(); // 检查分页
        // 2. 封装条件 执行查询
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.<AdChannel>lambdaQuery();

        // 频道名称
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdChannel::getName, dto.getName());
        }
        // 状态
        if (dto.getStatus() != null) {
            queryWrapper.eq(AdChannel::getStatus, dto.getStatus());
        }
        // 序号升序
        queryWrapper.orderByAsc(AdChannel::getOrd);
        // 分页
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        // 执行查询
        IPage<AdChannel> pageResult = this.page(pageReq, queryWrapper);
        // 3. 封装响应结果
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(), pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.校验参数
        //1.1 名称不能为空
        String name = adChannel.getName();
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能为空");
        }
        //1.2名称长度不能大于10
        if (name.length() > 10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称长度不能大于10");
        }
        //1.3 名称不能重复
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,name));
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道已经存在");
        }
        //2.频道保存到数据库
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //3. 返回成功的响应
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.参数校验
        //1.1 检查频道ID
        if (adChannel.getId()==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道id不能为空");
        }
        //1.2 根据id查询频道信息
        AdChannel oldChannel = getById(adChannel.getId());
        if (oldChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在");
        }
        //1.3 判断参数name是否为空，如果频道名称不为空，判断和原来的频道名称是否一致
        String name = adChannel.getName();
        //频道名称不为空，并且频道名称做了改变，需要校验一下频道名称
        if (StringUtils.isNotBlank(name)&&!name.equals(oldChannel.getName())){
            //名称长度不能大于10
            if (name.length() > 10){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称长度不能大于10");
            }
            //名称不能重复
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName,name));
            if (count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道已经存在");
            }
        }
        //2.修改频道
        updateById(adChannel);
        //3.响应结束
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
            //1.校验参数
            //根据id查询频道
            AdChannel channel = getById(id);
            if (channel == null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不存在");
            }
            //判断频道是否启用
            if (channel.getStatus()) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"频道有效，无法删除");
            }
            //如果未启用，删除频道
            removeById(id);
        //返回响应结果
        return ResponseResult.okResult();
    }
}